package com.atjg.autocat.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import java.io.IOException;
import java.util.List;

/* loaded from: classes9.dex */
public class WGCameraManage {
    Camera mCamera;
    SurfaceTexture mSurfaceTexture;

    public interface takePictureCallback {
        void onError(String str);

        void onSuccess(byte[] bArr);
    }

    public WGCameraManage(int mName) {
        this.mSurfaceTexture = new SurfaceTexture(0);
        this.mSurfaceTexture = new SurfaceTexture(mName);
    }

    public WGCameraManage() {
        this.mSurfaceTexture = new SurfaceTexture(0);
    }

    public void setmSurfaceTexture() {
    }

    public static Bitmap getimage(String srcPath) throws IOException {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        newOpts.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(srcPath, newOpts);
        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        int be = 1;
        if (w > h && w > 480.0f) {
            be = (int) (newOpts.outWidth / 480.0f);
        } else if (w < h && h > 800.0f) {
            be = (int) (newOpts.outHeight / 800.0f);
        }
        if (be <= 0) {
            be = 1;
        }
        newOpts.inSampleSize = be;
        Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
        return bitmap;
    }

    public String openCamera(int cameraId, SurfaceHolder surfaceHolder, Camera.PreviewCallback previewCallback) throws IOException {
        releaseCamera();
        boolean isExistCamera = false;
        Camera.CameraInfo info = new Camera.CameraInfo();
        int i = 0;
        while (true) {
            if (i >= Camera.getNumberOfCameras()) {
                break;
            }
            Camera.getCameraInfo(i, info);
            if (info.facing != cameraId) {
                i++;
            } else {
                isExistCamera = true;
                break;
            }
        }
        if (isExistCamera) {
            System.out.println("相机编号：" + cameraId);
            try {
                Camera cameraOpen = Camera.open(cameraId);
                this.mCamera = cameraOpen;
                int width = 0;
                int height = 0;
                Camera.Parameters mParameters = cameraOpen.getParameters();
                List<Camera.Size> sizeList = mParameters.getSupportedPreviewSizes();
                for (int i2 = 0; i2 < sizeList.size(); i2++) {
                    Camera.Size size = sizeList.get(i2);
                    if (size.width > width) {
                        width = size.width;
                        height = size.height;
                    }
                }
                mParameters.setPreviewSize(width, height);
                mParameters.setPictureSize(width, height);
                mParameters.setJpegQuality(50);
                this.mCamera.setParameters(mParameters);
                if (previewCallback != null) {
                    this.mCamera.setPreviewCallback(previewCallback);
                }
                try {
                    if (surfaceHolder != null) {
                        this.mCamera.setPreviewDisplay(surfaceHolder);
                    } else {
                        this.mCamera.setPreviewTexture(this.mSurfaceTexture);
                    }
                    this.mCamera.startPreview();
                    return null;
                } catch (Exception e) {
                    return "打开预览出错：" + e.getLocalizedMessage();
                }
            } catch (Exception e2) {
                return "相机初始化失败：" + e2.getLocalizedMessage();
            }
        }
        return "没找到对应相机";
    }

    public void takePicture(final takePictureCallback callback) {
        System.out.println("相机返回:" + this.mCamera + ":" + callback);
        if (this.mCamera == null) {
            if (callback != null) {
                callback.onError("相机未初始化成功");
                return;
            }
            return;
        }
        try {
            System.out.println("相机返回:aa:");
            this.mCamera.takePicture(null, null, null, new Camera.PictureCallback() { // from class: com.fth.car.mystartuser.utils.WGCameraManage.1
                @Override // android.hardware.Camera.PictureCallback
                public void onPictureTaken(byte[] data, Camera camera) {
                    callback.onError("相机+++++++++++++++++++++++++" + WGCameraManage.this.mCamera.toString());
                    System.out.println("相机返回:bb:");
                    takePictureCallback takepicturecallback = callback;
                    if (takepicturecallback != null) {
                        takepicturecallback.onSuccess(data);
                    }
                }
            });
        } catch (Exception e) {
            System.out.println("相机返回:cc" + e.toString() + ":");
            if (callback != null) {
                callback.onError(e.getLocalizedMessage());
            }
        }
    }

    public void releaseCamera() {
        Camera camera = this.mCamera;
        if (camera != null) {
            camera.stopPreview();
            this.mCamera.setPreviewCallback(null);
            this.mCamera.release();
            this.mCamera = null;
        }
    }
}